function getrRouteInfo(pathstart,pathend){
    var routeinfo = "前往地图";
    //x方向
    if(pathstart[0]<pathend[0]){
        routeinfo += "东";
    }else if(pathstart[0] > pathend[0]){
        routeinfo += "西";
    }
    //y方向
    if(pathstart[1]<pathend[1]){
        routeinfo += "南";
    }else if(pathstart[1] > pathend[1]){
        routeinfo += "北";
    }
    return routeinfo;
}
function switchFloor(floor){
	//切换地图
	switch(floor){
		case '1':
			$$('.map-container').css({'background-image':'url('+mapImageFloor1+')'});
			$$('.map-container').data('floor',floor);
			break;
		case '3':
			$$('.map-container').css({'background-image':'url('+mapImageFloor3+')'});
			$$('.map-container').data('floor',floor);
			break;
		default:
	}
	//选择楼层
	$$('.switch-btn').removeClass('active');
	$$('.switch-btn[floor="'+floor+'"]').addClass('active');
	//marker和信息框
	$$('#findPath .toolbar').hide();
	$$('.routeinfo').hide();
	$$('.marker').hide();
	$$('.marker.active[floor="'+floor+'"]').show();
	//清空画布并重新绘制路径
	$$('#floor-canvas').attr('height',mapH);
//	$$('.elevator-icon').hide();
	//路径规划起点层
	var sfloor = parseInt($$('.routeinfo').attr('floor'));
	switch(floor){
		case '1':
			if(pathroute.one){
				drawRoute(pathroute.one);
                var pathstart = pathroute.one[0];
                var pathend = pathroute.one[pathroute.one.length-1];
                var routeinfo = getrRouteInfo(pathstart,pathend);
				//两条路径同时存在，即路径跨层
				if(pathroute.three){
//					$$('.elevator-icon1').addClass('active');
//					$$('.elevator-icon1').show();
                    //不在起点层
                    if(sfloor && sfloor != 1){
                        routeinfo += "方处";
                        $$('.routeinfo .route-inner').html(routeinfo);
                        $$('.route-icon').hide();
                        $$('.routeinfo').show();
                    }
                    //起点层
                    else if(sfloor == 1){
                        routeinfo += "方电梯处，上3楼";
                        $$('.routeinfo .route-inner').html(routeinfo);
                        $$('.route-icon').show();
                        $$('.routeinfo').show();
                    }
				}else{
                    routeinfo += "方处";
                    $$('.routeinfo .route-inner').html(routeinfo);
                    $$('.route-icon').hide();
                    $$('.routeinfo').show();
				}
			}
			break;
		case '3':
			if(pathroute.three){
				drawRoute(pathroute.three);
                var pathstart = pathroute.three[0];
                var pathend = pathroute.three[pathroute.three.length-1];
                var routeinfo = getrRouteInfo(pathstart,pathend);
				if(pathroute.one){
//					$$('.elevator-icon3').addClass('active');
//					$$('.elevator-icon3').show();
                    if(sfloor && sfloor != 3){
                        routeinfo += "方处";
                        $$('.routeinfo .route-inner').html(routeinfo);
                        $$('.route-icon').hide();
                        $$('.routeinfo').show();
                    }
                    else if(sfloor == 3){
                        routeinfo += "方电梯处，下1楼";
                        $$('.routeinfo .route-inner').html(routeinfo);
                        $$('.route-icon').show();
                        $$('.routeinfo').show();
                    }
				}else{
                    routeinfo += "方处";
                    $$('.routeinfo .route-inner').html(routeinfo);
                    $$('.route-icon').hide();
                    $$('.routeinfo').show();
				}
			}
			break;
		default:
	}
}
function drawElevator(elebloc){
	//清除
	$$('.elevator-icon').hide();
	//坐标
	var elebloc1X = elebloc[1][0];
	var elebloc1Y = elebloc[1][1];
	var elebloc3X = elebloc[3][0];
	var elebloc3Y = elebloc[3][1];
	var ele1X = (elebloc1X+1)*netrectW-markerSize/2-netrectW/2;
	var ele1Y = (elebloc1Y+1)*netrectH-markerSize-netrectH/2;
	var ele3X = (elebloc3X+1)*netrectW-markerSize/2-netrectW/2;
	var ele3Y = (elebloc3Y+1)*netrectH-markerSize-netrectH/2;
	//添加
	$$('.elevator-icon1').transform('translate('+ele1X+'px,'+ele1Y+'px)');
	$$('.elevator-icon3').transform('translate('+ele3X+'px,'+ele3Y+'px)');
}
function getPathBlocks(blocks,start,end){
	grid = new PF.Grid(blocks);
    finder = new PF.AStarFinder({
    	allowDiagonal: true,
    	dontCrossCorners: true,
    	heuristic:PF.Heuristic.chebyshev
    });
    var path = finder.findPath(start[0], start[1], end[0], end[1], grid);
    path = PF.Util.smoothenPath(grid, path);
    //console.log(path);
    return path;
}

function drawRoute(path){
	//清空画布并重新绘制路径
	$$('#floor-canvas').attr('height',mapH);
	//获取起点
	var startposX = path[0][0]*netrectW+netrectW/2;
	var startposY = path[0][1]*netrectH+netrectH/2;
	//console.log(startposX,startposY);
	ctx.beginPath();
	ctx.strokeStyle = '#0000FF';//轮廓颜色
	ctx.lineWidth = 2;//路径宽度
    ctx.moveTo(startposX,startposY);//设置起点
    //console.log("起点=>"+startposX+' '+startposY);
    //绘制路径
	var bindex;
	for(bindex in path){
		var tragetX = path[bindex][0]*netrectW+netrectW/2;
		var tragetY = path[bindex][1]*netrectH+netrectH/2;
		ctx.lineTo(tragetX,tragetY);
   		//console.log(tragetX,tragetY);
	}
	ctx.stroke();
	//清空起点终点
	startBlock = null;
    endBlock = null;
}

function findPath(){
	$$('.routeinfo').hide();
	//起点终点在同一楼层
	if(startFloor == endFloor){
		switch(startFloor){
			case '1':
				pathroute.one = getPathBlocks(blocks1,startBlock,endBlock);
				pathroute.three = '';
				var mapimg = mapImageFloor1;
			    //绘制路径
			    //drawRoute(pathroute.one);
				break;
			case '3':
				pathroute.one = '';
			    pathroute.three = getPathBlocks(blocks3,startBlock,endBlock);
			    var mapimg = mapImageFloor3;
			    //绘制路径
			    //drawRoute(pathroute.three);
				break;
			default:
		}
		var routeinfo = getrRouteInfo(startBlock,endBlock);
        routeinfo += "方处";
        $$('.routeinfo .route-icon').hide();
	}
	//起点终点不在同一楼层
	else{
		//寻找起点楼层最近楼梯口
		var i;
		var dminelevator = 0;
		var dmin = 0;
		for(i in elevator){
			var x = parseFloat(elevator[i][startFloor][0]);
			var y = parseFloat(elevator[i][startFloor][1]);
			var cd = Math.pow(x-startBlock[0],2)+Math.pow(y-startBlock[1],2);
			//console.log(i,elevator[i][startFloor],cd);
			if(i==0){
				dmin = cd;
			}
			if(cd < dmin){
				dminelevator = i;
				dmin = cd;
			}
		}
		//console.log(dminelevator);
		//获取全部楼层路径
		switch(startFloor){
			case '1':
			    pathroute.one = getPathBlocks(blocks1,startBlock,elevator[dminelevator][1]);
			    pathroute.three = getPathBlocks(blocks3,elevator[dminelevator][3],endBlock);
			    var mapimg = mapImageFloor1;
			    var routeinfo = getrRouteInfo(startBlock,elevator[dminelevator][1]);
			    routeinfo += "方电梯处，上3楼";
				break;
			case '3':
				pathroute.three = getPathBlocks(blocks3,startBlock,elevator[dminelevator][3]);
				pathroute.one = getPathBlocks(blocks1,elevator[dminelevator][1],endBlock);
				var mapimg = mapImageFloor3;
				var routeinfo = getrRouteInfo(startBlock,elevator[dminelevator][3]);
			    routeinfo += "方电梯处，下1楼";
				break;
			default:
		}
        $$('.routeinfo .route-icon').show();
		//设定电梯位置
//		drawElevator(elevator[dminelevator]);
	}
	//跳转至起点楼层
    $$('.switch-btn').removeClass('active');
	$$('.switch-btn[floor="'+startFloor+'"]').addClass('active');
	$$('.map-container').css({'background-image':'url('+mapimg+')'});
	$$('.map-container').data('floor',startFloor);
	//marker
	$$('.marker').hide();
	$$('.marker.active[floor="'+startFloor+'"]').show();
    //信息框
    $$('.routeinfo .route-inner').html(routeinfo);
    $$('.routeinfo').attr('floor',startFloor);
    $$('.routeinfo').show();
	//清空画布并重新绘制路径
	$$('#floor-canvas').attr('height',mapH);
//	$$('.elevator-icon').hide();
	switch(startFloor){
		case '1':
			if(pathroute.one){
				drawRoute(pathroute.one);
				//两条路径同时存在，即路径跨层,显示电梯
//				if(pathroute.three){
//					$$('.elevator-icon1').addClass('active');
//					$$('.elevator-icon1').show();
//				}
			}
			break;
		case '3':
			if(pathroute.three){
				drawRoute(pathroute.three);
//				if(pathroute.one){
//					$$('.elevator-icon3').addClass('active');
//					$$('.elevator-icon3').show();
//				}
			}
			break;
		default:
	}
}

function setMarkerPos(markerselector,tapCenter,scale){
	var mapCtop  = parseFloat($$('.map-container').data('axisY'));
    var mapCleft = parseFloat($$('.map-container').data('axisX'));
    var mapCfloor = $$('.map-container').data('floor');
    //map-container中心点坐标(不随transform变换)
    var mapCenterX = mapW/2+mapCleft;
    var mapCenterY = mapH/2+mapCtop;
    //当前的map-container左上角顶点坐标
    var mapNewX = mapCenterX-mapW/2*scale;
    var mapNewY = mapCenterY-mapH/2*scale;
    //marker所处网格坐标
    var markerblockX = Math.round((tapCenter.x-mapNewX)/netrectW/scale)-1;
	var markerblockY = Math.round((tapCenter.y-mapNewY)/netrectH/scale)-1;
	//marker在父元素map-container变换前的坐标（相对于地图左上角顶点）
	var mleft = (markerblockX+1)*netrectW-netrectW/2-markerSize/2;
	var mtop  = (markerblockY+1)*netrectH-netrectH/2-markerSize;
	//console.log(markerblockX,markerblockY);
	//marker真实坐标
	var markerRealX = (tapCenter.x-mapNewX)/scale*mapRatioW;
	var markerRealY = (tapCenter.y-mapNewY)/scale*mapRatioH;
	//判断是否可选,并设定marker
	switch(mapCfloor){
		case '1':
			var cblocs = blocks1;
			break;
		case '3':
			var cblocs = blocks3;
			break;
		default:
			var cblocs = '';
	}
	if(cblocs){
		if(cblocs[markerblockY][markerblockX] == 0){
			//设定楼层
			$$(markerselector).attr('floor',mapCfloor);
			//设定位置
			$$(markerselector).transform('translate('+mleft+'px,'+mtop+'px)');
			$$(markerselector).addClass('active');
			$$(markerselector).show();
			var markerdisplay = true;
		}else{
			var markerdisplay = false;
		}
	}else{
		var markerdisplay = false;
	}
    //console.log('map('+mapNewX+','+mapNewY+');mapCenter('+mapCenterX+','+mapCenterY+');tap('+tapCenter.x+','+tapCenter.y+');scale('+scale+')');
    return {'markerdisplay':markerdisplay,'markerfloor':mapCfloor,'markerblock':[markerblockX,markerblockY],'markerpos':{'x':mleft,'y':mtop},'markerrealpos':{'x':markerRealX.toFixed(2),'y':markerRealY.toFixed(2)}};	
}

function setCurrentPos(){
	//清除定位点
    $$('.current-icon').hide();
    //添加新的定位点
    var floor = parseInt($$('.map-container').data('floor'));
    var cbx = parseInt($$('.current-icon').data('blockX'));
    var cby = parseInt($$('.current-icon').data('blockY'));
	var currentTop = cby*netrectH-currenticonSize+netrectH/2;
    var currentLeft = cbx*netrectW-currenticonSize/2+netrectW/2;
    //console.log(currentblock,netrectW,netrectH)
    $$('.current-icon').transform('translate('+currentLeft+'px,'+currentTop+'px)');
    $$('.current-icon').addClass('active');
    $$('.current-icon.active[floor="'+floor+'"]').show();
    //导航模式下，重新规划路径
    if($$('.walk-icon').hasClass('active')){
    	//选择定位点为起点
		startBlock = [cbx,cby];
		startFloor = $$('.current-icon').attr('floor');
		//选择当前点为终点
		endBlock = [parseInt($$('.walk-icon').data('navendblockX')),parseInt($$('.walk-icon').data('navendblockY'))];
		endFloor = $$('.walk-icon').data('navendFloor');
		//清空路径
		$$('#floor-canvas').attr('height',mapH);
		pathroute = {'one':'','three':''};
		//清空信息框和marker
		$$('#findPath .toolbar').hide();
		$$('.loc-icon-start').hide();
		$$('.loc-icon-start').removeClass('active');
//		$$('.elevator-icon').hide();
//		$$('.elevator-icon').removeClass('active');
		//console.log(startFloor,startBlock,endFloor,endBlock);
		//寻找路径
	  	findPath();
    }
}

function setStartPos(netpos,floor,pxpos){
	startBlock = netpos;
	startFloor = floor;
	//清空路径
	$$('#floor-canvas').attr('height',mapH);
	pathroute = {'one':'','three':''};
	//清空信息框和marker
	$$('#findPath .toolbar').hide();
	$$('.routeinfo').hide();
	$$('select[name="endpoint"]').val('0');
	//退出导航模式
	$$('.walk-icon').removeClass('active');
	$$('.loc-icon').hide();
	$$('.loc-icon').removeClass('active');
	$$('.loc-icon-start').hide();
	$$('.loc-icon-start').removeClass('active');
	$$('.loc-icon-end').hide();
	$$('.loc-icon-end').removeClass('active');
//	$$('.elevator-icon').hide();
//	$$('.elevator-icon').removeClass('active');
    //添加新marker
	$$('.loc-icon-start').attr('floor',startFloor);
	$$('.loc-icon-start').transform('translate('+pxpos.x+'px,'+pxpos.y+'px)');
	$$('.loc-icon-start').addClass('active');
	$$('.loc-icon-start').show();
}

function setEndPos(netpos,floor,pxpos){
	//起点必须存在
	if(startBlock){
    	endBlock = netpos;
    	endFloor = floor;
    	//清空信息框和marker
    	$$('#findPath .toolbar').hide();
    	$$('.routeinfo').hide();
    	$$('.loc-icon-end').hide();
    	$$('.loc-icon-end').removeClass('avtive');
    	$$('.loc-icon').hide();
    	$$('.loc-icon').removeClass('avtive');
    	//添加新marker
		$$('.loc-icon-end').attr('floor',endFloor);
    	$$('.loc-icon-end').transform('translate('+pxpos.x+'px,'+pxpos.y+'px)');
    	$$('.loc-icon-end').addClass('active');
    	$$('.loc-icon-end').show();
    	//寻找路径
  	  	findPath();
	}else{
		myApp.alert('请先选择起点');
	}
}

function setEndPos2(netpos,floor,pxpos){
	var status;
	if(startBlock){
		status = true;
	}else{
		var s = parseInt($$('select[name="startpoint"]').val());
		if(s != 0){
			status = true;
			startBlock = targetPoint[s].pos;
		}else{
			startBlock =false;
		}
	}
	//起点必须存在
	if(status){
    	endBlock = netpos;
    	endFloor = floor;
    	//清空信息框和marker
    	$$('#findPath .toolbar').hide();
//    	$$('.routeinfo').hide();
    	$$('.loc-icon-end').hide();
    	$$('.loc-icon-end').removeClass('avtive');
    	$$('.loc-icon').hide();
    	$$('.loc-icon').removeClass('avtive');
    	//添加新marker
		$$('.loc-icon-end').attr('floor',endFloor);
    	$$('.loc-icon-end').transform('translate('+pxpos.x+'px,'+pxpos.y+'px)');
    	$$('.loc-icon-end').addClass('active');
    	$$('.loc-icon-end').show();
    	//寻找路径
  	  	findPath();
	}else{
		setAutoNav(netpos,floor,pxpos);
	}
}
function setAutoNav(netpos,floor,pxpos){
	if(currentBlockX != 0 || currentBlockY != 0 ){
		//选择定位点为起点
		startBlock = [currentBlockX,currentBlockY];
		startFloor = $$('.current-icon').attr('floor');
		//选择当前点为终点
		endBlock = netpos;
		endFloor = floor;
		//设为导航模式
		$$('.walk-icon').addClass('active');
		$$('.walk-icon').data('navendblockX',endBlock[0]);
		$$('.walk-icon').data('navendblockY',endBlock[1]);
		$$('.walk-icon').data('navendFloor',endFloor);
		//清空路径
		$$('#floor-canvas').attr('height',mapH);
		pathroute = {'one':'','three':''};
		//清空信息框和marker
		$$('#findPath .toolbar').hide();
//		$$('.routeinfo').hide();
		$$('.loc-icon').hide();
		$$('.loc-icon').removeClass('active');
		$$('.loc-icon-start').hide();
		$$('.loc-icon-start').removeClass('active');
		$$('.loc-icon-end').hide();
		$$('.loc-icon-end').removeClass('active');
		$$('.elevator-icon').hide();
//		$$('.elevator-icon').removeClass('active');
		//添加新marker
		$$('.loc-icon-end').attr('floor',endFloor);
		$$('.loc-icon-end').transform('translate('+pxpos.x+'px,'+pxpos.y+'px)');
		$$('.loc-icon-end').addClass('active');
		$$('.loc-icon-end').show();
		//寻找路径
	  	findPath();
	}else{
		$$('select[name="endpoint"]').val('0');
		myApp.alert('无法确定您现在的位置');
	}
}